import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', 'staffService', 'ngDialog', 'ngDialogService')
class WorkerDocumentController extends BaseController {
  constructor() {
    super(staffService, 'queryDocument');

    this.$setQueryCallback((res) => {
      this.deviceDocumentData = res.rows;
    });

    this.mapParams = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
      documentName: '',
      documentNumber: '',
    };

    this.currentIndex = 0;
    this.$paginationQuery();
  }

  $reset() {
    this.mapParams = {
      orgId: this.$rootScope.orgId || storageApi.get('orgId'),
    };
  }

  $delete(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.staffService
          .deleteDocument([id])
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$paginationQuery();
            }
          });
      });
  }

  $addDocument(ctrl) {
    this.ngDialog.open({
      template: 'paAddItemDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, ngDialogService, staffService) {
        'ngInject';

        this.view = {
          attachmentFiles: [],
        };

        this.reqData = {
          orgId: $rootScope.orgId || storageApi.get('orgId'),
          documentName: '',
          documentNumber: '',
          compileTime: '',
          remark: '',
          attachmentIds: [],
        };

        this.submitForm = () => {
          this.reqData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          staffService.saveDocument(this.reqData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  /*eslint-disable */
  $viewDocument(params, ctrl) {
    this.ngDialog.open({
      template: 'paEditItemDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($scope, ngDialogService, staffService) {
        'ngInject';

        staffService.getDocument({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.document = res.data;
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.getDownloadSrc = (id) => {
          return `/sgms/api/v1/common/downLoad/${id}`;
        }
      },
    });
  }

  $editDocument(params, ctrl) {
    this.ngDialog.open({
      template: 'paEditItemDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller: function ctr($scope, ngDialogService, staffService) {
        'ngInject';

        // this.document = angular.copy(params);

        staffService.getDocument({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.document = res.data;
              this.view = {
                attachmentFiles: this.document.files,
              };
              this.view.attachmentFiles.forEach((item) => {
                item.name = item.fileName;
              });
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.submitEditForm = () => {
          this.document.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          staffService.updateDocument(this.document)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
                ctrl.$paginationQuery();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }
}

export default WorkerDocumentController;
